import { NodePool, Prefab, instantiate, Node } from "cc";


export default class PoolManager {
    private static _instance: PoolManager;
    public static getInstance(): PoolManager {
        if (!this._instance) {
            this._instance = new PoolManager();
        }
        return this._instance;
    }

    private poolDic: { [key: string]: Array<any> } = {};

    getPool(key: string): Array<any> {
        return this.poolDic[key];
    }

    getItem(key: string): any {
        let pool = this.poolDic[key];
        if (pool == null) {
            pool = [];
            this.poolDic[key] = pool;
        }
        if (pool.length > 0) {
            return pool.pop();
        }
        return null;
    }

    putItem(key: string, item: any) {
        let pool = this.poolDic[key];
        if (pool == null) {
            pool = [];
            this.poolDic[key] = pool;
        }
        if (pool.indexOf(item) == -1) {
            pool.push(item);
        }
    }
}